package com.android.sharecar.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * This activity shows the details information of user's real time pick ride request.
 * @author xinyan
 *@date 2011-10-13
 */
public class PickRideRequestDetailActivity extends Activity {
	public static final String TAG = "RealTimeRequestDetailActivity";

	// Views
	private Button mDialUser, mMessageUser, mShowRouteMap;
	private Button mAgreeToPickRide, mBackToList;
	private ImageView mHeadPic;
	private TextView mNickName, mGender;
	private TextView mStartPlace, mDestination;
	private TextView mPostTime;
	private TextView mPickRideNum;
	private TextView mPickRideSuccessTimes, mProvideRideSuccessTimes;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pickride_requestdetail);

		findViews();
		setListenerForViews();

	}

	private void findViews() {
		mDialUser = (Button) findViewById(R.id.pickRideRequestDetail_btn_dialUser);
		mMessageUser = (Button) findViewById(R.id.pickRideRequestDetail_btn_messageUser);
		mShowRouteMap = (Button) findViewById(R.id.pickRideRequestDetail_btn_showRouteMap);
		mAgreeToPickRide = (Button) findViewById(R.id.pickRideRequestDetail_btn_agreeToPickRide);
		mBackToList = (Button) findViewById(R.id.pickRideRequestDetail_btn_backToList);

		mHeadPic = (ImageView) findViewById(R.id.pickRideRequestDetail_iv_headPic);

		mPickRideNum = (TextView) findViewById(R.id.pickRideRequestDetail_tv_needSeats);
		mNickName = (TextView) findViewById(R.id.pickRideRequestDetail_tv_nickName);
		mGender = (TextView) findViewById(R.id.pickRideRequestDetail_tv_gender);
		mStartPlace = (TextView) findViewById(R.id.pickRideRequestDetail_tv_startLocation);
		mDestination = (TextView) findViewById(R.id.pickRideRequestDetail_tv_destination);
		mPostTime = (TextView) findViewById(R.id.pickRideRequestDetail_tv_postTime);
		mPickRideSuccessTimes = (TextView) findViewById(R.id.pickRideRequestDetail_tv_pickRideSuccessTimes);
		mProvideRideSuccessTimes = (TextView) findViewById(R.id.pickRideRequestDetail_tv_provideRideSuccessTimes);
	}

	private void setListenerForViews() {
		mDialUser.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					Intent dialIntent = new Intent();
					dialIntent.setAction(Intent.ACTION_CALL);
					dialIntent.setData(Uri.parse("tel:10086"));
					startActivityForResult(dialIntent, 1);

				} catch (ActivityNotFoundException e) {
					Log.e(TAG,
							"A ActivityNotFoundException catched when we gonna to dial a NO. Exception message:"
									+ e.getMessage());
					Toast.makeText(PickRideRequestDetailActivity.this,
							"对不起，找不到电话拨打组件，请检查您的系统是否感染病毒！", Toast.LENGTH_LONG)
							.show();
					return;
				} catch (Exception e) {
					Log.e(TAG,
							"A ecxception catched when we gonna to dial a NO. Exception message:"
									+ e.getMessage());
					Toast.makeText(PickRideRequestDetailActivity.this,
							"对不起，拨打电话失败，请检查您的手机", Toast.LENGTH_LONG).show();
					return;
				}
			}
		});

		mMessageUser.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				try {
					Uri smsToUri = Uri.parse("smsto:10086");
					Intent messageIntent = new Intent(
							android.content.Intent.ACTION_SENDTO, smsToUri);
					messageIntent.putExtra("sms_body", "请问你想搭我的车吗？？");
					startActivity(messageIntent);
				} catch (ActivityNotFoundException e) {
					Log.e(TAG,
							"A ActivityNotFoundException catched when we gonna tosend a message. Exception message:"
									+ e.getMessage());
					Toast.makeText(PickRideRequestDetailActivity.this,
							"对不起，找不到发送短信组件，请检查您的系统是否感染病毒！", Toast.LENGTH_LONG)
							.show();
					return;
				} catch (Exception e) {
					Log.e(TAG,
							"A ecxception catched when we gonna to send a message. Exception message:"
									+ e.getMessage());
					Toast.makeText(PickRideRequestDetailActivity.this,
							"对不起，发送短信失败，请检查您的手机", Toast.LENGTH_LONG).show();
					return;
				}
			}
		});

		mAgreeToPickRide.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		mBackToList.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				PickRideRequestDetailActivity.this.finish();
			}
		});
		mShowRouteMap.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Log.i(TAG, "Button ShowRouteMap clicked...");

				Intent intent = new Intent();
				intent.setClass(PickRideRequestDetailActivity.this,
						RouteMapActivity.class);

				startActivity(intent);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {

			// Here we should record this dial into the database.
			// then we gonna to show a hint for user
			new AlertDialog.Builder(this)
					.setTitle("提示")
					.setMessage(
							getResources().getString(
									R.string.agreeToPickRideHint))
					.setPositiveButton(
							getResources().getString(R.string.agreeToPickRide),
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {

								}
							})
					.setNegativeButton("取消",
							new DialogInterface.OnClickListener() {

								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									dialog.dismiss();
								}
							}).create().show();
		}// end of if resultcode ==
		super.onActivityResult(requestCode, resultCode, data);
	}

}
